package models;

import com.shove.Convert;
import play.Logger;
import play.db.jpa.JPA;
import play.db.jpa.Model;
import play.mvc.Scope;
import utils.Constants;
import utils.JPAUtil;
import utils.NumberUtil;
import utils.PageBean;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.Transient;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-04-14.
 */
@Entity
public class sys_user extends Model {
    public String wx_open_id;
    public String username;
    public String password;
    public String mobile;
    public String common;
    public long status;//账号状态1,正常0封号
    public Date created_date;
    @Transient
    public String roleName;
    public String getRoleName(long id){
        return "超级管理员";
    }
    public String getCreated_date() {
        return Convert.dateToStr(created_date,"");
    }
    public static PageBean<sys_user> select(StringBuilder sbSql, StringBuilder sbSqlCnt, Scope.Params params) {
        int currPage = Constants.ONE;
        int pageSize = Constants.PAGE_SIZE;
        String currPageStr = params.get(Constants.KEY_CURR_PAGE); // 当前页
        String pageSizeStr = params.get(Constants.KEY_PAGE_SIZE); // 分页行数
        if (NumberUtil.isNumericInt(currPageStr)) {
            currPage = Integer.parseInt(currPageStr);
        }
        if (NumberUtil.isNumericInt(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        try {
            EntityManager em = JPA.em();
            Query query = em.createNativeQuery(sbSql.toString(), customer_info.class);
            query.setFirstResult((currPage - 1) * pageSize);
            query.setMaxResults(pageSize);
            List<sys_user> customerInfoList = query.getResultList();
            String countStr = JPAUtil.createNativeQuery(sbSqlCnt.toString()).getSingleResult().toString();
            int count = Convert.strToInt(countStr,0);
            PageBean<sys_user> page = new PageBean();
            page.pageSize = pageSize;
            page.currPage = currPage;
            page.totalCount = count;
            page.page = customerInfoList;
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            Logger.error("BaseBiz.queryList.error:" + e.getMessage());
            return null;
        }
    }
}
